//Erstellt von Moritz Liepe
package main.simulation;

import main.IModuleControl;
import main.Module;

import org.eclipse.draw2d.Figure;
import org.eclipse.swt.widgets.Display;


public class CounterBehavior implements IBehavior 
{

	@Override
	public void simulate(Module module) 
	{
		for(int i = 0; i < module.getChildren().size(); i++)
		{
			Figure moduleChild  = (Figure)module.getChildren().get(i);
			if(moduleChild instanceof IModuleControl)
			{
				IModuleControl control = (IModuleControl)moduleChild ;
				
				if(control.getAddress().equals("counter"))
				{
					int count = 0;
					String oldValue = control.getValue();
					if((oldValue != null) && (oldValue.length() > 0))
					{
						try
						{
							count = Integer.parseInt(oldValue);
							count++;
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
					}
					
					Display.getDefault().asyncExec(new SetControlValue(control, Integer.toString(count)));
					
				}
			}
		}

	}

}
